package com.warom.sdg.service;

import android.app.Application;
import android.util.Log;

import com.google.gson.Gson;
import com.warom.sdg.model.ApiResponse;
import com.warom.sdg.model.ThresholdSettingEntity;
import com.warom.sdg.repository.ThresholdSettingRepository;
import com.warom.sdg.dto.ThresholdUpdateDto;

import java.util.List;

/**
 * 阈值设置服务类
 * 提供阈值设置相关的业务逻辑服务
 */
public class ThresholdSettingService {
    private static final String TAG = "ThresholdSettingService";
    private final ThresholdSettingRepository repository;
    private final Gson gson;

    public ThresholdSettingService(Application application) {
        this.repository = new ThresholdSettingRepository(application);
        this.gson = new Gson();
    }

    /**
     * 获取所有阈值设置
     * @return 包含所有阈值设置的API响应
     */
    public ApiResponse<List<ThresholdSettingEntity>> getThresholdSettings() {
        try {
            List<ThresholdSettingEntity> settings = repository.getAllThresholdSettings();
            return ApiResponse.success(settings);
        } catch (Exception e) {
            Log.e(TAG, "获取阈值设置失败", e);
            return ApiResponse.error("获取阈值设置失败: " + e.getMessage());
        }
    }

    /**
     * 更新单个阈值设置的范围值
     * @param updateDto 阈值更新数据传输对象
     * @return 包含更新结果的API响应
     */
    public ApiResponse<ThresholdSettingEntity> updateThresholdSetting(ThresholdUpdateDto updateDto) {
        try {
            Log.d(TAG, "接收到更新阈值范围请求: " + updateDto);
            
            if (updateDto == null || updateDto.getParamType() == null || updateDto.getParamType().isEmpty()) {
                Log.e(TAG, "参数类型不能为空");
                return ApiResponse.error("参数类型不能为空");
            }
            
            // 检查是否存在该参数类型的记录
            ThresholdSettingEntity existingEntity = repository.getThresholdSettingByParamType(updateDto.getParamType());
            if (existingEntity == null) {
                Log.e(TAG, "未找到参数类型为" + updateDto.getParamType() + "的阈值设置");
                return ApiResponse.error("未找到指定的阈值设置");
            }
            
            Log.d(TAG, "找到现有记录: " + existingEntity);
            
            // 仅更新阈值范围
            existingEntity.setMinValue(updateDto.getMinValue());
            existingEntity.setMaxValue(updateDto.getMaxValue());
            existingEntity.setUpdateTime(System.currentTimeMillis());
            
            Log.d(TAG, "准备更新的实体: " + existingEntity);
            boolean success = repository.saveThresholdSetting(existingEntity);
            Log.d(TAG, "更新结果: " + (success ? "成功" : "失败"));
            
            if (success) {
                // 重新获取更新后的数据
                ThresholdSettingEntity updatedEntity = repository.getThresholdSettingByParamType(updateDto.getParamType());
                Log.d(TAG, "更新后的实体: " + updatedEntity);
                return ApiResponse.success(updatedEntity);
            } else {
                Log.e(TAG, "更新阈值范围失败");
                return ApiResponse.error("更新阈值范围失败");
            }
        } catch (Exception e) {
            Log.e(TAG, "更新阈值范围失败", e);
            return ApiResponse.error("更新阈值范围失败: " + e.getMessage());
        }
    }

    /**
     * 更新单个阈值设置的范围值
     * @param jsonArgs JSON格式的阈值设置数据，必须包含paramType、minValue和maxValue
     * @return 包含更新结果的API响应
     */
    public ApiResponse<ThresholdSettingEntity> updateThresholdSetting(String jsonArgs) {
        try {
            Log.d(TAG, "接收到更新阈值范围请求: " + jsonArgs);
            ThresholdUpdateDto updateDto = gson.fromJson(jsonArgs, ThresholdUpdateDto.class);
            Log.d(TAG, "解析后的更新数据: " + updateDto);
            
            return updateThresholdSetting(updateDto);
        } catch (Exception e) {
            Log.e(TAG, "解析JSON数据失败", e);
            return ApiResponse.error("解析请求数据失败: " + e.getMessage());
        }
    }
} 